package com.hsh.gaoqianlianmeng.utils;

import com.hsh.gaoqianlianmeng.constants.DateTimeConstants;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;

@Data
@Slf4j
public class DateUtils {

    /**
     * str 转为 localDate
     *
     * @param dateStr
     * @return
     */
    public static LocalDate strToDate(String dateStr) {
        String datePatten = DateTimeConstants.PURE_DATE_PATTERN;
        return strToDate(dateStr, datePatten);
    }

    /**
     * str 转为 localDate
     *
     * @param dateStr
     * @param datePatten
     * @return
     */
    public static LocalDate strToDate(String dateStr, String datePatten) {
        LocalDate localDate = null;
        try {
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern(datePatten);
            // 尝试解析字符串为LocalDate
            localDate = LocalDate.parse(dateStr, formatter);
        } catch (DateTimeParseException e) {
            log.error("strToDate->日期转换失败，str->{}，patten->{}", dateStr, datePatten);
        }
        return localDate;
    }

    /**
     * str 转为 localDate
     *
     * @param dateStr
     * @param datePatten
     * @return
     */
    public static LocalDateTime strToDateTime(String dateStr, String datePatten) {
        LocalDateTime localDateTime = null;
        try {
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern(datePatten);
            // 尝试解析字符串为LocalDate
            localDateTime = LocalDateTime.parse(dateStr, formatter);
        } catch (DateTimeParseException e) {
            log.error("strToDateTime->日期转换失败，str->{}，patten->{}", dateStr, datePatten);
        }
        return localDateTime;
    }

    /**
     * localDate 转为 str
     *
     * @param localDate
     * @param datePatten
     * @return
     */
    public static String dateToStr(LocalDate localDate, String datePatten) {
        String dateStr = null;
        try {
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern(datePatten);
            // 尝试解析字符串为LocalDate
            dateStr = localDate.format(formatter);
        } catch (DateTimeParseException e) {
            log.error("dateToStr->日期转换失败，localDate->{}，patten->{}", localDate, datePatten);
        }
        return dateStr;
    }


    /**
     * 将固定格式的时间转为指定的格式
     *
     * @param dateStr    yyyyMMdd
     * @param datePatten 指定格式
     * @return
     */
    public static String changeDateStrPatten(String dateStr, String datePatten) {
        LocalDate localDate = strToDate(dateStr);
        if (localDate == null) {
            return dateStr;
        }
        return dateToStr(localDate, datePatten);
    }

    /**
     * localDateTime 转为 str
     *
     * @return
     */
    public static String getNowDateTimeStr() {
        String dateStr = null;
        LocalDateTime localDateTime = LocalDateTime.now();
        String datePatten = DateTimeConstants.PURE_DATETIME_MS_PATTERN;
        try {
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern(datePatten);
            // 尝试解析字符串为LocalDate
            dateStr = localDateTime.format(formatter);
        } catch (DateTimeParseException e) {
            log.error("getNowStr->日期转换失败，localDateTime->{}，patten->{}", localDateTime, datePatten);
        }
        return dateStr;
    }
}
